package com.javasm.dao.impl;

import com.javasm.dao.MenuDao;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.entity.sql.LoginManageSql;
import com.javasm.entity.sql.MenuManageSql;
import com.javasm.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/15 19:49
 * @since JDK 1.8
 */
public class MenuDaoImpl implements MenuDao {
    /**
     * 通过每个用户的权限列表查询该用户的对应权限
     *
     * @param menuId 权限列表
     * @return
     */
    @Override
    public List<AdminMenu> selectList(String menuId) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<AdminMenu> adminMenus = null;
        StringBuilder builder = new StringBuilder(LoginManageSql.SELECT_MENU_BY_POWER);
        builder.append(menuId);
        builder.append(")");
        try {
            adminMenus = queryRunner.query(builder.toString(), new BeanListHandler<>(AdminMenu.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminMenus;
    }

    /**
     * 查询菜单首选项信息
     *
     * @return
     */
    @Override
    public List<AdminMenu> selectMenuHead() {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<AdminMenu> adminMenuList = null;
        try {
            adminMenuList = queryRunner.query(MenuManageSql.SELECT_MENU_HEAD, new BeanListHandler<>(AdminMenu.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminMenuList;
    }

    /**
     * 通过父id查询子菜单信息
     *
     * @return
     */
    @Override
    public List<AdminMenu> selectMenuByPid(Integer pid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<AdminMenu> adminMenuList = null;
        try {
            adminMenuList = queryRunner.query(MenuManageSql.SELECT_MENU_BY_PID, new BeanListHandler<>(AdminMenu.class), pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminMenuList;
    }

    /**
     * 查询所有权限信息，用于权限管理的树形结构，fid = 0
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectMenuKey() {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Map<String, Object>> menuHead = null;
        try {
            menuHead = queryRunner.query(MenuManageSql.SELECT_MENU_KEY, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return menuHead;
    }

    /**
     * 查询所有权限信息，用于权限管理的树形结构，fid = 父节点的pid
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectChildMenu(int pid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Map<String, Object>> childMenu = null;
        try {
            childMenu = queryRunner.query(MenuManageSql.SELECT_MENU_KEY_BY_FID, new MapListHandler(),pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return childMenu;
    }


}
